package com.agricultural.proxy;

import com.agricultural.environment.EnvironmentMonitor;
import com.agricultural.facade.AgriculturalSystemFacade;
import com.agricultural.security.SecurityManager;

// 代理模式：安全代理，控制对系统外观的访问
public class SecureAgriculturalSystemFacade extends AgriculturalSystemFacade {
    private AgriculturalSystemFacade realFacade;
    private SecurityManager securityManager;

    public SecureAgriculturalSystemFacade() {
        this.realFacade = new AgriculturalSystemFacade();
        this.securityManager = SecurityManager.getInstance();
    }

    @Override
    public void initializeSystem() {
        System.out.println("进入initializeSystem方法");
        if (securityManager.hasPermission("ADMIN")) {
            realFacade.initializeSystem();
        } else {
            System.out.println("权限不足，需要ADMIN角色才能初始化系统");
        }
        System.out.println("离开initializeSystem方法");
    }

    @Override
    public void shutdownSystem() {
        if (securityManager.hasPermission("ADMIN")) {
            realFacade.shutdownSystem();
        } else {
            System.out.println("权限不足，需要ADMIN角色才能关闭系统");
        }
    }

    @Override
    public void createEquipment(String type, String id) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.createEquipment(type, id);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能创建设备");
        }
    }

    @Override
    public void startEquipment(String equipmentId) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.startEquipment(equipmentId);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能启动设备");
        }
    }

    @Override
    public void stopEquipment(String equipmentId) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.stopEquipment(equipmentId);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能停止设备");
        }
    }

    @Override
    public void performTask(String equipmentId) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.performTask(equipmentId);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能执行任务");
        }
    }

    @Override
    public void refuelEquipment(String equipmentId) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.refuelEquipment(equipmentId);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能加油");
        }
    }

    @Override
    public void handleEmergency(String equipmentId, String emergencyType) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.handleEmergency(equipmentId, emergencyType);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能处理紧急情况");
        }
    }

    @Override
    public void switchPathPlanner(boolean isWetField) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.switchPathPlanner(isWetField);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能切换路径规划器");
        }
    }

    @Override
    public void calculatePath(double[][] fieldBoundary, double[] startPosition) {
        if (securityManager.hasPermission("OPERATOR")) {
            realFacade.calculatePath(fieldBoundary, startPosition);
        } else {
            System.out.println("权限不足，需要OPERATOR角色才能计算路径");
        }
    }

    @Override
    public void showCommandHistory() {
        if (securityManager.hasPermission("VIEWER")) {
            realFacade.showCommandHistory();
        } else {
            System.out.println("权限不足，需要VIEWER角色才能查看命令历史");
        }
    }

    // 添加环境监测相关方法的代理
    @Override
    public void startMonitoring() {
        if (securityManager.hasPermission("ADMIN")) {
            realFacade.startMonitoring();
        } else {
            System.out.println("权限不足，需要ADMIN角色才能启动环境监测");
        }
    }

    @Override
    public void stopMonitoring() {
        if (securityManager.hasPermission("ADMIN")) {
            realFacade.stopMonitoring();
        } else {
            System.out.println("权限不足，需要ADMIN角色才能停止环境监测");
        }
    }
}